package ruifeng.zcj.dig.ui.forum;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewHolder;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;
import ruifeng.zcj.dig.R;
import ruifeng.zcj.dig.base.DepositFragmentActivity;
import ruifeng.zcj.dig.base.MyApp;
import ruifeng.zcj.dig.base.api.ApiHttpClient;
import ruifeng.zcj.dig.ui.base.RefreshRecyclerFragment;
import ruifeng.zcj.dig.ui.forum.bean.NewPostDetail;
import ruifeng.zcj.dig.ui.shopping.bean.ShoppingList;
import ruifeng.zcj.dig.ui.shopping.bean.TwoCategory;
import ruifeng.zcj.dig.ui.vehicle.bean.NewVehicle;
import ruifeng.zcj.mybase.base.BaseFragment;
import ruifeng.zcj.mybase.base.utils.GlideUtil;
import ruifeng.zcj.mybase.base.utils2.TimeUtil;

/**
 * 帖子的评论，所有人的评论
 * Created by Administrator on 2016/10/12 0012.
 */
public class ForumDetailListFragment extends RefreshRecyclerFragment {
    protected ForumDetailActivity mForumDetailActivity;
    protected String content;
    MyApp myApps;

    /**
     * 配置eventbus
     **/
    protected void initUIConfig(FragmentConfig fragmentConfig) {
        fragmentConfig.isApplyEventBus = true;
    }

    @Override
    protected void initFragmentConfig(View view) {
        super.initFragmentConfig(view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mForumDetailActivity = (ForumDetailActivity) mActivity;

        myApps = (MyApp)mApp;


    }

    @Override
    protected BGARecyclerViewAdapter getAdapter() {
        return new MomentAdapter(recyclerView);
    }

    @Override
    protected long getAutoRefreshTime() {
        // 最新商品分类两小时刷新一次
        return 2 * 60 * 60;
    }

    @Override
    public void onRVItemClick(ViewGroup parent, View itemView, int position) {

    }

    @Override
    public boolean onRVItemLongClick(ViewGroup parent, View itemView, int position) {
        return false;
    }

    @Override
    protected void onUserVisible() {
    }

    // 去取新数据
    protected void sendRequestData() {
        if (mMomentAdapter != null && mForumDetailActivity.mNewPostDetail != null) {
            mMomentAdapter.addNewData(mForumDetailActivity.mNewPostDetail.getPostingscomment());
        }
    }

    public class MomentAdapter extends BGARecyclerViewAdapter<NewPostDetail.PostingscommentBean> {
        public MomentAdapter(RecyclerView recyclerView) {
            super(recyclerView, R.layout.listview_item_details);
        }

        @Override
        public void fillData(BGAViewHolderHelper helper, int position, NewPostDetail.PostingscommentBean moment) {
            /*if (TextUtils.isEmpty(moment.content)) {
                helper.setVisibility(R.id.tv_item_moment_content, View.GONE);
            } else {
                helper.setVisibility(R.id.tv_item_moment_content, View.VISIBLE);
                helper.setText(R.id.tv_item_moment_content, moment.content);
            }*/


            ImageView imageView = helper.getImageView(R.id.iv_head);
            GlideUtil.loadImage(mContext, "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png", imageView);


            helper.setText(R.id.vehicle_fragment_item_title, "" + moment.getUsername());
            helper.setText(R.id.vehicle_fragment_item_address, "" + moment.getTime());
            helper.setText(R.id.vehicle_fragment_item_price1, ""+moment.getComment());

          /*
            /*TextView postingsname = helper.getView(R.id.forum_fragment_item_text_title);

            BGANinePhotoLayout ninePhotoLayout = helper.getView(R.id.npl_item_moment_photos);

            ninePhotoLayout.init(MomentListActivity.this);
            ninePhotoLayout.setDelegate(MomentListActivity.this);
            ninePhotoLayout.setData(moment.photos);*/
        }

        @Override
        public void onViewRecycled(BGARecyclerViewHolder holder) {
            super.onViewRecycled(holder);

           /* BGANinePhotoLayout ninePhotoLayout = holder.getViewHolderHelper().getView(R.id.npl_item_moment_photos);
            ninePhotoLayout.init(null);
            ninePhotoLayout.setDelegate(null);*/
        }
    }

    /**
     * 与发布者在同一个线程
     * 注意事项：
     * 1.事件被post后哪个事件的监听者会响应的问题，这个取决于你写的监听方法所跟的参数类型。
     * 2.你写的事件监听方法得是public修饰的，如果你写成private了，则不会收到消息。
     * 3.如果你所有的事件监听方法都不用public修饰（包括父类），则会直接报错：
     *
     * @param content 事件2
     */
    @Subscribe
    public void onEvent(String content) {
        this.content = content;
        if (!TextUtils.isEmpty(content)) {
            NewPostDetail.PostingscommentBean newVehicle = new NewPostDetail.PostingscommentBean();
            newVehicle.setUsername(" ");
            newVehicle.setComment(content);
            newVehicle.setTime(TimeUtil.getNowMDHMSTime());
            if (!TextUtils.isEmpty(myApps.getUserId())){
                newVehicle.setFuserid(myApps.getUserId());
            }else {
                newVehicle.setFuserid("空");
            }
            if (mMomentAdapter != null) {
                mMomentAdapter.addFirstItem(newVehicle);
            }
        }

    }

    /**
     * 与发布者在同一个线程
     * 注意事项：
     * 1.事件被post后哪个事件的监听者会响应的问题，这个取决于你写的监听方法所跟的参数类型。
     * 2.你写的事件监听方法得是public修饰的，如果你写成private了，则不会收到消息。
     * 3.如果你所有的事件监听方法都不用public修饰（包括父类），则会直接报错：
     *
     * @param mNewPostDetail 事件2
     */
    @Subscribe
    public void onEvent(NewPostDetail mNewPostDetail) {
        if (mMomentAdapter != null && mNewPostDetail != null) {
            List<NewPostDetail.PostingscommentBean> mss =   mNewPostDetail.getPostingscomment();
            mMomentAdapter.addNewData(mss);
        }
    }



    /*@Override
    protected void onFirstUserVisible() {
        super.onFirstUserVisible();


    }*/
}
